<template>
  <div class="page-box h100 display-flex czbj">
    <p class="title-label mb10">{{jdsfdfs}}--订购管理</p>
    <div>
      <div class="display-flex">
        <div class="zxczxertert">
          <n-input v-model:value="jnxsdfd.cardName" type="text" placeholder="卡片名称" />
        </div>
        <div class="zxczxertert ml20">
          <n-select v-model:value="jnxsdfd.cardStatus" placeholder="卡片状态" :options="jcfdsfs" />
        </div>
        <div class="zxczxertert ml20">
          <n-select v-model:value="jnxsdfd.shopId" placeholder="所属店铺" :options="mendianl" />
        </div>


        <div class="ml20">
          <n-button type="info" @click="initsd" :loading="jxcfdfdf"> 搜索 </n-button>
          <!-- <n-button type="info" @click="hf('addka')" class="ml20"> +添加 </n-button> -->
        </div>
      </div>

    </div>
    <n-data-table :border="true" class="mt20" :columns="columns" :data="shopInfos" :bordered="false" />

    <div class="mt20    display-flex">
      <div class="flex-1"></div>
      <div class="cdfvdfsxgrtr">
        <n-pagination v-model:page="jnxsdfd.page" :page-count="totalNum" @update:page='xzcsdertrt' />
      </div>
    </div>
    <!-- <div class="mt20 z9 fz12">
      
      新增或修改可能不会立即生效，需要等待10秒-5分钟同步  
    </div> -->
  </div>
</template>
<script lang='ts' setup>
import { ref, reactive, h } from 'vue'
import { dxget, hf, dialog, dxpost } from "../../util/index";
import { NTag, NButton } from 'naive-ui';
const shopInfos = ref([])
const jxcfdfdf = ref(false)
const mendianl = ref([])
const knmxfdf =ref(false)
const jdsfdfs = localStorage.sjmcsee
let jcfdsfs = [
  { label: '全部', value: '' },
  { label: "订购中", value: "init" },
  { label: "正常", value: "available" },
  { label: "已用完", value: "used_up" },
  { label: "关闭中", value: "closing" },
  { label: "已关闭", value: "closed" },
  { label: "已暂停", value: "pause" },

]
const columns = ref(
  [
    { title: '订购号', key: 'cardId',width:180, },
    { title: '用户名称', key: 'userName', align: "center" },
    { title: '商品名称', key: 'name',
    render(row) {
      return h('div',{class:'dfsfsdrtrt'},row.name)
    },
     },
    
    
    {
      title: '卡售价', key: 'salePriceTotal', align: "center",
      render(row) {
        if (row.salePriceTotal) {
          return row.salePriceTotal / 100 + '元'
        }
        return '--'

      }
    },
    {
      title: '已核销金额', key: 'usedCash',
      align: "center",
      render(row) {
        if (row.usedCash) {
          return row.usedCash / 100 + '元'
        }
        return '--'

      }
    },
    {
      title: '退款金额', key: 'refundCash', align: "center", render(row) {
        if (row.refundCash) {
          return row.refundCash / 100 + '元'
        }
        return '--'

      }
    },
    { title: '总期数或次数', key: 'totalCount', align: "center" },
    { title: '剩余期数或次数', key: 'remainCount', align: "center" },

    {
      title: '售卖订单状态', key: 'cardStatus',
      align: "center",
      render(row) {
        let kjdfds = jcfdsfs.filter(a => a.value == row.cardStatus)[0].label
        let vsser = jcfdsfs.filter(a => a.value == row.cardStatus)[0].value
        let kdfdswert = 'info'
        if (vsser == 'used_up') {
          kdfdswert = 'warning'
        }
        if (vsser == 'pause') {
          kdfdswert = 'warning'
        }
        if (vsser == 'closed') {
          kdfdswert = 'error'
        }
        return h(NTag, { type: kdfdswert }, kjdfds)
      }

    },


    { title: '创建时间', key: 'createDate' },
    {
      title: '操作', align: "center", render(row) {
        let jfdsfds= [
        h(NButton, {
            type: 'primary',
            size: 'small',
            quaternary: true,
            onClick: () => {
              hf('kapxq',{
                cardId: row.cardId,
                userId: row.userId,
                openId:row.openId
              })
            }
          }, '查看详情')
        ]
        if(row.cardStatus=='pause'){
        
          jfdsfds.push(h(NButton, {
            type: 'primary',
            size: 'small',
            quaternary: true,
            loading:knmxfdf.value,
            onClick: async() => {
             
              const kdfdsf= await dialog('该订单恢复后将会重新扣款，请确认是否恢复。')
              if(kdfdsf==1){
                knmxfdf.value = true
                const jdfdsfw = await dxpost('meirong/wantu/merchantcardset', {
                  openId:row.openId, 
                  userId:row.userId,
                  cardId: row.cardId,operationType:'RECOVER' 
                })
                knmxfdf.value = false
                if(jdfdsfw.data.code !=10000){
                  window.$message.error(jdfdsfw.data.subMsg)
                  return
                }
                
                initsd()
                window.$message.success('恢复成功,请等待10秒左右生效')
              }
            }
          }, '恢复'))
          
        }



        if(row.cardStatus=='available'){
          jfdsfds.push(h(NButton, {
            type: 'primary',
            size: 'small',
            quaternary: true,
            loading:knmxfdf.value,
            onClick: async() => {
              const kdfdsf= await dialog('该订单解约后将不会继续扣款，请确认是否解约。')
              if(kdfdsf==1){
                knmxfdf.value = true
                const jdfdsf=  await dxpost('meirong/wantu/merchantcardclose',{
                cardId:row.cardId,
                openId:row.openId, 
                userId:row.userId,
              })

              knmxfdf.value = false
                 if(jdfdsf.data.code !=10000){
                  window.$message.error(jdfdsf.data.subMsg)
                  return
                }
                initsd()
               
                window.$message.success('解约成功,请等待10秒左右生效')


              }
            
              // alert('正在开发中...')
            }
          }, '解约'))
          jfdsfds.push(h(NButton, {
            type: 'primary',
            size: 'small',
            quaternary: true,
            loading:knmxfdf.value,
            onClick: async() => {
             
              const kdfdsf= await dialog('该订单暂定后将不会继续扣款，请确认是否暂停。')
              if(kdfdsf==1){
                knmxfdf.value = true
                const jdfdsf= await dxpost('meirong/wantu/merchantcardset', {
                  openId:row.openId, 
                  userId:row.userId,
                  cardId: row.cardId,
                  operationType:'PAUSE'
                 })
                 knmxfdf.value = false
                 if(jdfdsf.data.code !=10000){
                  window.$message.error(jdfdsf.data.subMsg)
                  return
                }
                initsd()
               
                window.$message.success('暂停成功,请等待10秒左右生效')
              }
            }
          }, '暂停'))
          
        }
        return jfdsfds
      }
    },

  ]
)

const totalNum = ref()

const jnxsdfd = ref({
  cardName: "",
  cardStatus: null,
  shopId: null,
  page: 1
})
const zxcxzrtret = ref(false)
const zhjhsd = [

  { label: '已发布', value: 'PUBLISHED' },
  { label: '审核中', value: 'REVIEWING' },
  { label: '审核失败', value: 'REJECTED' },
  { label: '已下架', value: 'DELISTING' },
  { label: '已冻结', value: 'FREEZING' },
  { label: '已过期', value: 'EXPIRED' },

]
const jnxdfd = [
  { label: '统一收款', value: '1' },
  { label: '分店结算', value: '2' },
]
const jzxcsdfd = (valus) => {
  return zhjhsd.filter(a => a.value == valus)[0].label
}
const jzxcsdfdb = (valus) => {
  try {

    return jnxdfd.filter(a => a.value == valus)[0].label
  } catch (e) {

  }
}
const initsd = async () => {
  jxcfdfdf.value = true
  const { data } = await dxget('meirong/wantu/orderbatchquery', jnxsdfd.value)
  jxcfdfdf.value = false
  shopInfos.value = data.content
  totalNum.value = data.totalNum
  // pagination.value.pageCount = data.totalSize
}
initsd()
const xzcsdertrt = (data) => {
  jnxsdfd.value.page = data
  initsd()
}
const shanchuser = async (data) => {
  const res = await dialog("确认删除吗？");
  if (res == 1) {
    //等于1为确认删除了
    const ret = await dxpost('meirong/wantu/sanchudp', { shopId: data.shopId })
    window.$message.success('删除成功！')
    initsd()
  }
}
const xiazai = async (sd, operateStatus) => {
  zxcxzrtret.value = true
  const { data } = await dxpost('meirong/wantu/xiugmodify', { cardTemplateId: sd.cardTemplateId, operateStatus })
  console.log(data);
  zxcxzrtret.value = false
  if (data.code != 10000) {
    window.$message.error(data.subMsg)
    return
  }


  initsd()
}
const mendianList = async () => {
  const { data } = await dxget("meirong/wantu/mendianList")

  console.log(data);
  mendianl.value = data
}
mendianList()
</script>
<style scoped>
.zxczxertert {
  width: 300px;
}
</style>